package com.newland.starpos.partner.utils;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by liuNW on 2018/9/30.
 */
public class IPUtils {

    public static String getRealIP(HttpServletRequest request) {
        //多次反向代理后会有多个ip值，第一个ip才是真实ip
        String ip = request.getHeader("X-Forwarded-For");
        if(StringUtils.isNotBlank(ip) && !"unKnown".equalsIgnoreCase(ip)){
            int index = ip.indexOf(",");
            if(index == -1)
                return ip;
            else
                return ip.substring(0,index);
        }
        ip = request.getHeader("X-Real-IP");
        if(StringUtils.isNotBlank(ip) && !"unKnown".equalsIgnoreCase(ip))
            return ip;
        return request.getRemoteAddr();
    }
}
